//
//  NSString+Pinyin.m
//  OA
//
//  Created by Guo Rui on 2017/12/5.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "NSString+Pinyin.h"

@implementation NSString (Pinyin)
- (NSString *)transfromToPinyin
{
    NSMutableString *str = [NSMutableString stringWithString:self];
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
    // 转成不带声调的拼音
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);
    
    return str.copy;
}

- (NSString *)transfromToFuzzysearch:(NSString *)pinyin
{
    NSArray *pinyinArray = [pinyin componentsSeparatedByString:@" "];
    NSMutableString *allString = [NSMutableString new];
    
    for (NSString *s in pinyinArray) {
        [allString appendFormat:@"#%@", s];
    }
    
    NSMutableString *initialStr = [NSMutableString new];
    for (NSString *s in pinyinArray) {
        if (s.length > 0) {
            [initialStr appendString:[s substringToIndex:1]];
        }
    }
    
    [allString appendFormat:@"#%@", initialStr];
    [allString appendFormat:@"#%@", self];
    
    return allString;
}
@end
